<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getAdminRoleList">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'admin_role_save'" type="primary" :icon="CirclePlus" @click="openDrawer('新增')">新增角色</el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button v-auth="'admin_role_save'" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">
          编辑
        </el-button>
        <el-button v-auth="'admin_role_del'" type="danger" link :icon="Delete" @click="deleteAdminRole(scope.row)">
          删除
        </el-button>
      </template>
    </ProTable>
    <RoleEdit ref="drawerRef" />
  </div>
</template>
<script setup lang="tsx" name="roleManage">
import { ref, reactive } from "vue";
import { System } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import RoleEdit from "@/views/system/roleManage/RoleEdit.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import { getAdminRoleList, changeAdminRoleStatus, saveAdminRole, delAdminRole } from "@/api/modules/system";
import { userStatus } from "@/utils/dict";
import { useAuthButtons } from "@/hooks/useAuthButtons";
import { useHandleData } from "@/hooks/useHandleData";
// ProTable 实例
const proTable = ref<ProTableInstance>();
//按钮权限
const { BUTTONS } = useAuthButtons();
// 表格配置项
const columns = reactive<ColumnProps<System.AdminRole>[]>([
  { prop: "name", label: "角色名称", search: { el: "input" } },
  {
    prop: "status",
    label: "状态",
    tag: true,
    enum: userStatus,
    search: { el: "select-v2", props: { filterable: true } },
    fieldNames: { label: "label", value: "value" },
    render: scope => {
      return (
        <>
          {BUTTONS.value.admin_role_status ? (
            <el-switch
              model-value={scope.row.status}
              active-text={scope.row.status ? "启用" : "禁用"}
              active-value={1}
              inactive-value={0}
              onClick={() => changeStatus(scope.row)}
            />
          ) : (
            <el-tag type={scope.row.status ? "success" : "danger"}>{scope.row.status ? "启用" : "禁用"}</el-tag>
          )}
        </>
      );
    }
  },
  { prop: "createdAt", label: "创建时间" },
  { prop: "updatedAt", label: "更新时间" },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
]);
//切换角色状态
const changeStatus = async (row: System.AdminRole) => {
  await useHandleData(changeAdminRoleStatus, { id: row.id, status: row.status == 1 ? 0 : 1 }, `切换【${row.name}】角色状态`);
  proTable.value?.getTableList();
};

// 删除权限资源
const deleteAdminRole = async (params: System.AdminRole) => {
  await useHandleData(delAdminRole, { ids: params.id }, `删除【${params.name}】角色`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof RoleEdit> | null>(null);
const openDrawer = (title: string, row: Partial<System.AdminRole> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: saveAdminRole,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
